package kr.pe.osdev.dao.jdbc.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import kr.pe.osdev.dao.CommentaryDao;
import kr.pe.osdev.dao.jdbc.BaseJdbcDao;
import kr.pe.osdev.domain.Commentary;
import kr.pe.osdev.domain.Question;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class CommentaryDaoImpl extends BaseJdbcDao implements CommentaryDao {

    @Override
    public Commentary add(Commentary commentary) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("commentary.add");

        Object[] obj = new Object[] { commentary.getSid(), commentary.getDescription(), commentary.getQuestionSid(),
                commentary.getUseYn() };

        getJdbcTemplate().update(sql, obj);

        return commentary;
    }

    @Override
    public int count() throws DataAccessException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Commentary findBySid(Commentary commentary) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("commentary.findBySid");
        
        return (Commentary) getJdbcTemplate().queryForObject(sql, new Object[] { commentary.getSid() }, getRowMapper());
    }
    
    @Override
    public Commentary findByQuestion(Question question) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("commentary.findByQuestionSid");
        
        Commentary commentary = null;
        
        try {
            commentary = (Commentary) getJdbcTemplate().queryForObject(sql, new Object[] { question.getSid() }, getRowMapper());
        } catch(EmptyResultDataAccessException e) {
            commentary = new Commentary();
        }
        
        return commentary;
    }

    @Override
    public List<Commentary> findList() throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int modify(Commentary commentary) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("commentary.modify");
        
        Object[] obj = new Object[] { commentary.getDescription(), commentary.getUseYn(), commentary.getSid()};
        
        getJdbcTemplate().update(sql, obj);
        
        return getJdbcTemplate().update(sql, obj);
    }

    @Override
    public Commentary remove(Commentary commentary) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    private RowMapper getRowMapper() {
        RowMapper rowMapper = new RowMapper() {
            public Object mapRow(ResultSet rs, int rownum) throws SQLException {
                return makeCommentary(rs, false);
            }
        };
        
        return rowMapper;
    }

    private Commentary makeCommentary(ResultSet rs, boolean mode) throws SQLException {
        Commentary commentary = new Commentary();
        commentary.setSid(rs.getString("SID"));
        commentary.setDescription(rs.getString("DESCRIPTION"));
        commentary.setQuestionSid(rs.getString("QUESTION_SID"));
        commentary.setUseYn(rs.getString("USE_YN"));
        
        return commentary;
    }
}
